<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="发票种类" prop="invoiceType.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择发票种类"
            labelName = 'id'
            labelValue = 'id'
            :value = "inputForm.invoiceType.id"
            :limit="1"
            @getValue='(value) => {inputForm.invoiceType.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'name',
              label: '名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'name',
              label: '名称'
            }
            ]"
            dataListUrl="/epc/invoicetype/invoiceType/list"
            entityBeanName="invoiceType"
            queryEntityUrl="/epc/invoicetype/invoiceType/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票日期" prop="issueDate"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.issueDate"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发票代码" prop="invoiceCode"
                :rules="[
                 ]">
              <el-input v-model="inputForm.invoiceCode" placeholder="请填写发票代码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发票号码" prop="invoiceNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.invoiceNumber" placeholder="请填写发票号码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="不含税金额" prop="priceExcludingTax"
                :rules="[
                 ]">
              <el-input v-model="inputForm.priceExcludingTax" placeholder="请填写不含税金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税额" prop="tax"
                :rules="[
                 ]">
              <el-input v-model="inputForm.tax" placeholder="请填写税额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="价税合计" prop="totalPriceWithTax"
                :rules="[
                 ]">
              <el-input v-model="inputForm.totalPriceWithTax" placeholder="请填写价税合计"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否作废" prop="isCancellation"
                :rules="[
                 ]">
                <el-select v-model="inputForm.isCancellation" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.clientName" placeholder="请填写客户名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="纳税人识别号" prop="taxpayerRegistrationNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxpayerRegistrationNumber" placeholder="请填写纳税人识别号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="地址" prop="address"
                :rules="[
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="电话" prop="phone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开户行" prop="openingBank"
                :rules="[
                 ]">
              <el-input v-model="inputForm.openingBank" placeholder="请填写开户行"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="账号" prop="accountNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.accountNumber" placeholder="请填写账号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="附件" prop="file"
                :rules="[
                 ]">
              <el-upload ref="file"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/epc/invoice/invoice`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.file = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.file = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="fileArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        fileArra: [],
        inputForm: {
          id: '',
          invoiceType: {
            id: ''
          },
          issueDate: '',
          invoiceCode: '',
          invoiceNumber: '',
          priceExcludingTax: '',
          tax: '',
          totalPriceWithTax: '',
          isCancellation: '',
          clientName: '',
          taxpayerRegistrationNumber: '',
          address: '',
          phone: '',
          openingBank: '',
          accountNumber: '',
          file: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建发票`
        } else if (method === 'edit') {
          this.title = '修改发票'
        } else if (method === 'view') {
          this.title = '查看发票'
        }
        this.fileArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/epc/invoice/invoice/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.invoice)
              this.inputForm.file.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.fileArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/epc/invoice/invoice/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>